//
//  MoodViewController.m
//  Today
//
//  Created by fanfan on 15/5/22.
//  Copyright (c) 2015年 fanfan. All rights reserved.
//

#import "MoodViewController.h"

@interface MoodViewController ()

@end

@implementation MoodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //  [tool deleteMoodTable];
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    //    myTableView.backgroundColor = [UIColor whiteColor];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, myTableView.frame.size.height)];
    UIImage *image = [UIImage imageNamed:@"niupizhi.jpg"];
    imageView.image = image;
    [self.view addSubview:imageView];
    imageView.userInteractionEnabled = YES;
    [imageView release];
    
    
    [imageView addSubview:myTableView];
    [myTableView release];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


- (void)sendContent:(NSString *)str
{
    
    if (str.length != 0) {
        [self.array addObject:str];
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.textLabel.text = [self.array objectAtIndex: indexPath.row];
    UIImage *image = [UIImage imageNamed:@"iconfont-jiahao"];
    cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cellButton.frame = CGRectMake(self.view.frame.size.width - 30, 0, 20, 70);
    [cellButton setBackgroundImage:image forState:UIControlStateNormal];
    [cellButton addTarget:self action:@selector(touchCellButton:) forControlEvents:UIControlEventTouchUpInside];
    cellButton.tag = indexPath.row;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"niupizhi.jpg"]];
    cell.backgroundView =imageView;
    [cell addSubview:cellButton];
    
    
    
    return cell;
}
- (void)touchCellButton:(UIButton *)btn
{
    [MoodDataBaseTool openCollectDB];
    [MoodDataBaseTool createCollectDB];
    NSString *string = [self.array objectAtIndex:btn.tag];
    NSLog(@"%@", string);
    
    //   NSLog(@"%ld", btn.tag);
    
    NSArray *array = [NSArray array];
    array = [MoodDataBaseTool selectCollectDB];
    NSInteger n = 0;
    for (int i = 0; i < array.count; i++) {
        if ([string isEqualToString:array[i]]) {
            NSLog(@"已经添加收藏");
            n = 1;
            break;
        }
    }
    
    if (n == 0) {
        [MoodDataBaseTool insertCollectDB:string];
        NSLog(@"添加收藏");
        SIAlertView *alert = [[SIAlertView alloc] initWithTitle:nil andMessage:@"添加收藏"];
        [alert addButtonWithTitle:@"确定" type:SIAlertViewButtonTypeCancel handler:^(SIAlertView *alertView) {
            
        }];
        alert.transitionStyle = SIAlertViewTransitionStyleSlideFromTop;
        [alert show];
    }else if (n == 1)
    {
        SIAlertView *alert = [[SIAlertView alloc] initWithTitle:nil andMessage:@"已经添加收藏"];
        [alert addButtonWithTitle:@"确定" type:SIAlertViewButtonTypeCancel handler:^(SIAlertView *alertView) {
            
        }];
        alert.transitionStyle = SIAlertViewTransitionStyleSlideFromTop;
        [alert show];
        
    }
}
#pragma mark 启动编辑动画
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [myTableView setEditing: editing animated:animated];
}
#pragma mark -
#pragma mark 允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#pragma mark -
#pragma mark 编辑方式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
#pragma mark -
#pragma mark 确认编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete ) {
        //首先删除数组中的数据
        NSString *string = [self.array objectAtIndex:indexPath.row];
        [MoodDataBaseTool deleteMoodDB:string];
        [self.array removeObjectAtIndex:indexPath.row];
        [myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [myTableView reloadData];
    NSArray *array = [NSArray array];
    array = [MoodDataBaseTool selectMoodDB];
    self.array = [NSMutableArray arrayWithArray:array];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoodSecondViewController *second = [[MoodSecondViewController alloc] init];
    NSString *string = [self.array objectAtIndex:indexPath.row];
    second.cellContent = string;
    //   second.mySendTextDelegate = self;
    [self.navigationController pushViewController:second animated:YES];
    [second release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
